package com.app.sysmodule.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Created by song on 2017/9/26.
 */
public class JsonData {
    private static final Logger logger = Logger.getLogger(JsonData.class);
    public static JSONObject getRequestBodyData(HttpServletRequest request){
        StringBuffer json = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            request.setCharacterEncoding("UTF-8");
            reader = request.getReader();
            while((line = reader.readLine()) != null) {
                json.append(line);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            logger.error("getRequestBodyData 获取客户端参数失败:"+e.getMessage());
        }finally {
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("getRequestBodyData 关闭流失败:"+e.getMessage());
                }
            }
        }
        return JSONObject.parseObject(json.toString());
    }
}
